import {
  queryCustomerSalPriceList,
  queryMaterialList,
  querySalesperson,
  queryTBdCustomerList,
  queryViewItem
} from '@/api/marketManage/marketOrder'
import {
  queryDepartment,
  queryPaymentList,
  querySettletype
} from '@/api/marketManage/customer/createCustomer'
import {
  queryFbillType, queryTBdStock,
  queryTBdSupplier
} from '@/api/purchaseManagement/createPurchasePrice'
import {
  queryBuyerList,
  FNOTEqueryStockerList
} from '@/api/purchaseManagement/purchaseOrder'
import { queryTJxRoleList, userslist } from '@/api/basicManagement/userslist'
import { queryBaseCurrencyList, queryExchangeRateList } from '@/api/basicManagement/currencyRates'

export default {
  data() {
    return {
      costomerData: [], // 客户
      deliverData: [], // 交货方式
      marketsData: [], // 销售员
      gatherData: [], // 收款条件
      currencysData: [], // 收款条件
      materialsData: [], // 物料列表
      supplierData1: [], // 供应商
      tableSettlement: [], // 付款条件
      purchaseData1: [], // 采购员
      tableStockerData: [], // 仓管员
      salesDeta: [], // 采购部门
      tableSettlType: [], // 结算方式
      purchaseTypeData: [], // 采购类型
      ruleData: [], // 角色
      marPriceData: [], // 销售价目
      applicantData: [], // 申请人（用户列表）
      rateTypeData: [], // 汇率类型
      warehouCkData: [] // 仓库
    }
  },
  methods: {
    // 获取客户数据
    async queryTBdCustomerList() {
      const DATA = {
        fdocumentStatus: 'C',
        fforbidstatus: 'A',
        pageNum: 1, // 当前页
        pageSize: 100000 // 每页显示多少条数据
      }
      if (this.costomerData.length > 0) {
        return
      }
      const { data: RES } = await queryTBdCustomerList(DATA)
      this.costomerData = RES.array
    },
    querySearch(queryString, cb) {
      const restaurants = this.costomerData
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.fname.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        )
      }
    },
    // 交货方式
    async queryFheadDelivery() {
      const DATA = {
        viewName: 'SX_V_SAL_DELIVERY',
        pageNum: 1, // 当前页
        pageSize: 100 // 每页显示多少条数据
      }
      if (this.deliverData.length > 0) {
        return
      }
      const { data: RES } = await queryViewItem(DATA)
      this.deliverData = RES.list
    },
    querySearch2(queryString, cb) {
      const restaurants = this.deliverData
      const results = queryString
        ? restaurants.filter(this.createFilter2(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter2(queryString) {
      return restaurant => {
        return (
          restaurant.fname.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        )
      }
    },
    // 销售员
    async querySalesperson(ev) {
      this.ismarketlDialog = true
      const DATA = {
        staffType: ev || 1, // 1-销售员, 2-采购员
        pageNum: 1, // 当前页
        pageSize: 100 // 每页显示多少条数据
      }
      if (this.marketsData.length > 0) {
        return
      }
      const { data: RES } = await querySalesperson(DATA)
      this.marketsData = RES.list
    },
    querySearch3(queryString, cb) {
      const restaurants = this.marketsData
      const results = queryString
        ? restaurants.filter(this.createFilter3(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter3(queryString) {
      return restaurant => {
        return (
          restaurant.fuserName.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    // 收款条件
    async querytReccondition() {
      const DATA = {
        viewName: 'SX_V_SAL_COLLECTION',
        pageNum: 1, // 当前页
        pageSize: 100 // 每页显示多少条数据
      }
      if (this.gatherData.length > 0) {
        return
      }
      const { data: RES } = await queryViewItem(DATA)
      this.gatherData = RES.list
    },
    querySearch4(queryString, cb) {
      const restaurants = this.gatherData
      const results = queryString
        ? restaurants.filter(this.createFilter4(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter4(queryString) {
      return restaurant => {
        return (
          restaurant.fname.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0 ||
          restaurant.fnumber
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0 ||
          restaurant.fdescripTion
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0
        )
      }
    },
    // 结算币别
    async queryTBdCurrency(index) {
      const DATA = {
        pageNum: 1, // 当前页
        pageSize: 100 // 每页显示多少条数据
      }
      index >= 0 ? this.tableIndex = index : ''
      if (this.currencysData.length > 0) {
        return
      }
      const { data: RES } = await queryBaseCurrencyList(DATA)
      this.currencysData = RES.list
    },
    querySearch5(queryString, cb) {
      const restaurants = this.currencysData
      const results = queryString
        ? restaurants.filter(this.createFilter5(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter5(queryString) {
      return restaurant => {
        return (
          restaurant.fname.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0 ||
          restaurant.fnumber
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0 ||
          restaurant.fcode.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        )
      }
    },
    // 物料列表(销售)
    async queryMaterialList(index) {
      this.material = index
      const item = this.tabTwo.saleDetails[index]
      const DATA = {
        fnumber: item.fnumber || '',
        pageNum: 1, // 当前页
        pageSize: 20 // 每页显示多少条数据
      }
      const { data: RES } = await queryMaterialList(DATA)
      this.materialsData = RES.array
    },
    // 物料列表(采购)
    async queryMaterialList1(index) {
      this.indexSelf = index
      const item = this.saleDetails[index]
      const DATA = {
        fnumber: item.fnumber || '',
        pageNum: 1, // 当前页
        pageSize: 20 // 每页显示多少条数据
      }
      const { data: RES } = await queryMaterialList(DATA)
      this.materialsData = RES.array
    },
    querySearch6(queryString, cb) {
      const restaurants = this.materialsData
      const results = queryString
        ? restaurants.filter(this.createFilter6(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter6(queryString) {
      return restaurant => {
        return (
          restaurant.fnumber
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0 ||
          restaurant.fmodel.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0 ||
          restaurant.fdescripTion
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0
        )
      }
    },
    // 付款条件
    async handleSettlement() {
      const DATA = {
        pageNum: 1, // 当前页
        pageSize: 200 // 每页显示多少条数据
      }
      if (this.tableSettlement.length > 0) {
        return
      }
      const { data: RES } = await queryPaymentList(DATA)
      this.tableSettlement = RES.array
    },
    querySearch7(queryString, cb) {
      const restaurants = this.tableSettlement
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 结算方式
    async querySettletype() {
      const DATA = {
        pageNum: 1, // 当前页
        pageSize: 200 // 每页显示多少条数据
      }
      if (this.tableSettlType.length > 0) {
        return
      }
      const { data: RES } = await querySettletype(DATA)
      this.tableSettlType = RES.array
    },
    querySearch8(queryString, cb) {
      const restaurants = this.tableSettlType
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 供应商
    async handleSupplier(index) {
      index || index === 0 ? this.materielIndex = index : ''
      const DATA = {
        pageNum: 1, // 当前页
        pageSize: 2000 // 每页显示多少条数据
      }
      if (this.supplierData1.length > 0) {
        return
      }
      const { data: RES } = await queryTBdSupplier(DATA)
      this.supplierData1 = RES.array
    },
    querySearch9(queryString, cb) {
      const restaurants = this.supplierData1
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 采购员
    async handlePurchase() {
      const DATA = {
        pageNum: 1, // 当前页
        pageSize: 2000 // 每页显示多少条数据
      }
      if (this.purchaseData1.length > 0) {
        return
      }
      const { data: RES } = await queryBuyerList(DATA)
      this.purchaseData1 = RES.list
    },
    querySearch10(queryString, cb) {
      const restaurants = this.purchaseData1
      const results = queryString
        ? restaurants.filter(this.createFilter10(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter10(queryString) {
      return restaurant => {
        return (
          restaurant.FNAME.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        )
      }
    },
    // 仓管员
    async queryStocker() {
      const DATA = {
        staffType: 3,
        pageNum: 1, // 当前页
        pageSize: 2000 // 每页显示多少条数据
      }
      if (this.tableStockerData.length > 0) {
        return
      }
      await FNOTEqueryStockerList(DATA).then(res => {
        if (res.code === 0) {
          this.tableStockerData = res.data.list
        }
      })
    },
    querySearch11(queryString, cb) {
      const restaurants = this.tableStockerData
      const results = queryString
        ? restaurants.filter(this.createFilter3(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 销售部门
    async handleDepartment() {
      const { data: res } = await queryDepartment({
        pageNum: 1, // 当前页
        pageSize: 2000 // 每页显示多少条数据
      })
      this.salesDeta = res.array
    },
    querySearch12(queryString, cb) {
      const restaurants = this.salesDeta
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 采购类型
    async handlePurType() {
      if (this.purchaseTypeData.length > 0) {
        return
      }
      const { data: res } = await queryFbillType({
        pageNum: 1, // 当前页
        pageSize: 200 // 每页显示多少条数据
      })
      this.purchaseTypeData = res.array
    },
    querySearch15(queryString, cb) {
      const restaurants = this.purchaseTypeData
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 角色
    async handleRule() {
      if (this.ruleData.length > 0) {
        return
      }
      const { data: res } = await queryTJxRoleList({
        pageNum: 1, // 当前页
        pageSize: 200 // 每页显示多少条数据
      })
      this.ruleData = res.array
    },
    querySearch16(queryString, cb) {
      const restaurants = this.ruleData
      const results = queryString
        ? restaurants.filter(this.createFilter11(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter11(queryString) {
      return restaurant => {
        return (
          restaurant.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0
        )
      }
    },
    // 销售价目表
    async queryPricerList() {
      if (this.marPriceData.length > 0) {
        return
      }
      if (!this.prodValue.fsettleCurrId) {
        this.$message({
          showClose: true,
          message: '请选择结算币别',
          type: 'warning'
        })
        return
      }
      const { data: res } = await queryCustomerSalPriceList({
        fcurrencyId: this.prodValue.fsettleCurrId,
        pageNum: 1, // 当前页
        pageSize: 2000 // 每页显示多少条数据
      })
      this.marPriceData = res.list
    },
    querySearch17(queryString, cb) {
      const restaurants = this.marPriceData
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 基本单位 (数据字典公共接口)
    async getBasicUnit(ev, index, en) {
      if (index || index === 0) {
        this.materielIndex = index
        this.indexSelf = index
      }
      en ? this.gys = en : ''
      this.funit = ev
      const DATA = { viewName: ev, isAll: ev === 'SX_V_BASEUNIT' ? 1 : 0 }
      const { data: RES } = await queryViewItem(DATA)
      this.unitData = RES.list
    },
    querySearch18(queryString, cb) {
      const restaurants = this.unitData
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 获取申请人
    async handleApplicant(ev) {
      if (ev === 0 || ev) this.indexSelf = ev
      if (this.applicantData.length > 0) {
        return
      }
      const { data: RES } = await userslist({
        formId: 'sec_user',
        pageNum: 1, // 当前页
        pageSize: 2000 // 每页显示多少条数据
      })
      const { array } = RES
      this.applicantData = array
    },
    querySearch19(queryString, cb) {
      const restaurants = this.applicantData
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 查询汇率类型
    async queryRateType() {
      if (this.rateTypeData.length > 0) {
        return
      }
      const DATA = this.currency
      const { data: RES } = await queryExchangeRateList(DATA)
      this.rateTypeData = RES.list
    },
    querySearch20(queryString, cb) {
      const restaurants = this.rateTypeData
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    // 仓库列表
    async handleWarehouse(index) {
      if (index || index === 0) {
        this.materielIndex = index
        this.indexSelf = index
      }
      if (this.warehouCkData.length > 0) {
        return
      }
      const { data: RES } = await queryTBdStock({
        pageNum: 1, // 当前页
        pageSize: 2000 // 每页显示多少条数据
      })
      this.warehouCkData = RES.list
    },
    querySearch21(queryString, cb) {
      const restaurants = this.warehouCkData
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    }
  }
}
